﻿/************************************************************************

	File:			pak_io_win32.c
	Version:		1.0
	Changes:		A
	Author:			Stanley Lau
	Date:			2010.07.27
	Purpose:		File IO Module win32-platform Functions Implements

************************************************************************/

#include "pak_io_win32.h"


FILE* _OpenFile_win32( const char* pstrFileName, const char* pstrMode )
{
	int		bResult = 0;
	FILE*	pFile = NULL;
	TCHAR*	ptszFileName = NULL, *ptszMode = NULL;
	size_t	nFileNameLen = 0, nModeLen = 0;

	PROCESS_ERROR( pstrFileName && pstrMode );
	nFileNameLen = _U8ToT( pstrFileName, -1, 0, 0 );
	nModeLen = _U8ToT( pstrMode, -1, 0, 0 );
	PROCESS_ERROR( nFileNameLen && nModeLen );

	PROCESS_ERROR( ptszFileName = (TCHAR*)malloc( sizeof(TCHAR) * ( nFileNameLen + 1 ) ) );
	PROCESS_ERROR( ptszMode = (TCHAR*)malloc( sizeof(TCHAR) * ( nModeLen + 1 ) ) );
	
	_U8ToT( pstrFileName, -1, ptszFileName, nFileNameLen );
	_U8ToT( pstrMode, -1, ptszMode, nModeLen );

	PROCESS_ERROR( pFile = _tfopen( ptszFileName, ptszMode ) );
	bResult = 1;
Exit0:
	if( 0==bResult )
	{
		if( pFile ) fclose( pFile );
		pFile = NULL;
	}

	if( ptszFileName )
		free( ptszFileName );
	if( ptszMode )
		free( ptszMode );
	return pFile;
}

int _CreateDir_win32( const char* pszDirName )
{
	int			bResult = 0;
	LPTSTR		ptszDirName = NULL;
	LPTSTR		ptszInterDirName = NULL;
	LPTSTR		ptszPos = NULL, ptszPrevDelimiChar = NULL, ptszDelimiChar = NULL;
	size_t		ntDirNameLen = 0;

	PROCESS_ERROR( pszDirName );
	
	ntDirNameLen = _U8ToT( pszDirName, -1, NULL, 0 );
	PROCESS_ERROR( ptszDirName = (LPTSTR)malloc( sizeof(TCHAR) * ntDirNameLen ) );
	PROCESS_ERROR( ptszInterDirName = (LPTSTR)malloc( sizeof(TCHAR) * ntDirNameLen ) );
	_U8ToT( pszDirName, -1, ptszDirName, ntDirNameLen );

	ptszPos = ptszPrevDelimiChar = ptszDelimiChar = ptszDirName;
	while( ptszDelimiChar = _tcschr( ptszPos, _T('\\') ) )
	{
		size_t nInterDirLen = 0;
		PROCESS_ERROR( ptszDelimiChar - ptszPrevDelimiChar );
		nInterDirLen = ptszDelimiChar - ptszDirName;
		_tcsncpy( ptszInterDirName, ptszDirName, nInterDirLen );
		ptszInterDirName[nInterDirLen] = 0;

		if( !_IsDirExist_win32T( ptszInterDirName ) )
		{
			PROCESS_ERROR( CreateDirectory( ptszInterDirName, NULL ) );
		}
		
		ptszPrevDelimiChar = ptszDelimiChar;
		ptszPos = ptszDelimiChar + 1;
	}
	if( !_IsDirExist_win32T( ptszDirName ) )
	{
		PROCESS_ERROR( CreateDirectory( ptszDirName, NULL ) );
	}
	/*ptszDelimiChar = _tcschr( ptszDirName, _T('\\') );
	while( ptszDelimiChar )
	{
		size_t nInterDirLen = 0;
		nInterDirLen = ptszDelimiChar - ptszDirName;
		if( 0==nInterDirLen )
		{

		}
	}*/
	//PROCESS_ERROR( CreateDirectory( ptszDirName, NULL ) );

	bResult = 1;
Exit0:

	if( ptszDirName )
		free( ptszDirName );
	if( ptszInterDirName )
		free( ptszInterDirName );
	return bResult;
}

int _IsDirExist_win32( const char* pszDirName )
{
	int			bResult = 0;
	LPTSTR		ptszDirName = NULL;
	size_t		ntDirNameLen = 0;

	PROCESS_ERROR( pszDirName );

	ntDirNameLen = _U8ToT( pszDirName, -1, NULL, 0 );
	PROCESS_ERROR( ptszDirName = (LPTSTR)malloc( sizeof(TCHAR) * ntDirNameLen ) );
	_U8ToT( pszDirName, -1, ptszDirName, ntDirNameLen );

	bResult = _IsDirExist_win32T( ptszDirName ); 

Exit0:
	if( ptszDirName )
		free( ptszDirName );
	return bResult;
}

int _IsDirExist_win32T( LPCTSTR ptszDirName )
{
	int		bResult = 0;
	HANDLE	hDir = INVALID_HANDLE_VALUE;

	PROCESS_ERROR( ptszDirName );

	hDir = CreateFile( ptszDirName, 
		GENERIC_READ, 
		FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
		NULL, 
		OPEN_EXISTING, 
		FILE_FLAG_BACKUP_SEMANTICS, 
		NULL );


	bResult = (hDir!=INVALID_HANDLE_VALUE);
Exit0:
	if( hDir!=INVALID_HANDLE_VALUE )
	{
		CloseHandle( hDir );
		hDir = INVALID_HANDLE_VALUE;
	}
	return bResult;
}
//Last line for linux

